/**
 * 详情对话框
 */
var StageDataInfoDlg = {
    data: {
        id: "",
        stageId: "",
        videoUrl: "",
        dataUrl: "",
        knowledgeContentIds: "",
        videoTitle: "",
        status: "",
        sort: "",
        remark: ""
    }
};

layui.use(['form', 'admin', 'upload', 'element', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    var element = layui.element;
    var progressLayer;

    getSpecialtyType();

    //获取专业类型并展示
    function getSpecialtyType() {
        var ajax = new $ax(Feng.ctxPath + "/specialtyType/showList");
        var result = ajax.start();
        result.data.map(item =>{
            $('#specialtyId').append('<option value="'+item.id+'">'+item.typeName+'</option>');
        });
        form.render('select');
    }

    form.on('select(specialtyId)', function(data){
        var stageid = $('#stageId').val();
        $('#stageId').html("");
        var ajax = new $ax(Feng.ctxPath + "/stage/stageList?specialtyId="+data.value);
        var result = ajax.start();
        result.data.map(item =>{
            var selectedY = '';
            if(stageid==item.id){
                selectedY = 'selected';
            }
            $('#stageId').append('<option '+ selectedY +' value="'+item.id+'">'+item.chapters+'</option>');
        });
        form.render('select');
    });

    /**
     * 展示知识点
     */
    $("#knowledgeContentNames").click(function(){
        if($('#specialtyId').val() != ""){
            layer.open({
                type: 2,
                title: '知识点',
                area: ['600px', '600px'], //宽高
                fix: false,
                maxmin: true,
                content: Feng.ctxPath + "/knowledgeContent/knowledgeContentList/" + $('#specialtyId').val()
            });
        }else{
            layer.msg("请先选择专业类型");
        }
    })

    $("#videoBtn").click(function(){
        if($('#specialtyId').val() == ''|| $('#stageId').val() == '' || $('#knowledgeContentIds').val() == ''){
            layer.msg("请先选择好专业、阶段和知识点后再进行上传！！！")
            return false;
        }
        $("#videoUrl").click();
    })

    $("#fileBtn").click(function(){
        if($('#specialtyId').val() == ''|| $('#stageId').val() == '' || $('#knowledgeContentIds').val() == ''){
            layer.msg("请先选择好专业、阶段和知识点后再进行上传！！！")
            return false;
        }
        $("#dataUrl").click()
    })

    //上传视频
    upload.render({
        elem: '#videoUrl'
        , url: Feng.ctxPath + '/video/upload/'
        , accept: 'video'
        ,data:{}
        , before: function (obj) {
            this.data = {"specialtyId": $('#specialtyId').val(),"stageId": $('#stageId').val(),"knowledgeIds": $('#knowledgeContentIds').val()};
            progressLayer = top.layer.msg('不是卡了，数据上传中...',{icon: 16,time:false,shade:0.8});
            obj.preview(function (index, file, result) {

            });
        }
        , done: function (res) {
            top.layer.close(progressLayer);
            $("#videoLabel").after('<div class="layui-input-block videoDiv">\n' +
                '                            <img src="/video'+res.data.poster+'" class="videoImg">\n' +
                '                            <p class="videoP">'+res.data.title+'</p>\n' +
                '                            <i class="layui-icon videoI">&#x1006;</i>\n' +
                '                            <input type="hidden" name="videoIds" value="'+res.data.id+'">\n' +
                '                        </div>');
            form.render();
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传视频失败！");
        }
    });

    $(document).on("click",'.videoI', function(){
        console.log($(this).parent());
        $(this).parent().remove();
    })

    //上传资料
    upload.render({
        elem: '#dataUrl'
        , url: Feng.ctxPath + '/stage/upload/'
        , accept: 'file'
        ,data:{}
        , before: function (obj) {
            this.data = {"specialtyId": $('#specialtyId').val(),"stageId": $('#stageId').val(),"knowledgeIds": $('#knowledgeContentIds').val()};
            obj.preview(function (index, file, result) {
                $("#dataLabel").after('<div class="layui-input-block" style="border:1px solid;width: 80px; float: left; height: 80px;margin: 0 10px 5px 0;font-size: 12px;"><p style="width: 75px;word-break: break-all;word-wrap: break-word;">'+file.name+'</p></div>');
            });
        }
        , done: function (res) {
            /*$("#videoUrl").val(res.data.fileId);*/
            dataIds.push(res.data.fileId);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传文件失败！");
        }
    });

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/stageData/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val("stageDataForm", {
        "specialtyId": result.data.specialtyId
        ,"knowledgeContentNames": result.data.title
        ,"id": result.data.id
        ,"stageId": result.data.stageId
        ,"dataUrl": result.data.dataUrl
        ,"videoUrl": result.data.videoUrl
        ,"knowledgeContentIds": result.data.knowledgeContentIds
        ,"chapters": result.data.chapters
        ,"videoTitle": result.data.videoTitle
        ,"status": result.data.status
        ,"sort": result.data.sort
        ,"remark": result.data.remark
    });
    $('#stageId').append('<option value="'+result.data.id+'">'+result.data.chapters+'</option>');
    var select = 'dd[lay-value=' + result.data.specialtyId + ']';
    $('#specialtyId').siblings("div.layui-form-select").find('dl').find(select).click();//触发数据值点击事件

    form.render(); //更新全部

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/stageData/editItem", function (data) {
            Feng.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

});